<template>
  <div class="app-container">
    <el-form :model="queryParams" :rules="rules" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教师姓名" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师职务" prop="position">
        <el-select v-model="queryParams.position" placeholder="请选择教师职务" clearable>
          <el-option
            v-for="dict in dict.type.position"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:teacher:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:teacher:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNumber" />
      <el-table-column label="教师姓名" align="center" prop="teacherName" />
      <el-table-column label="教师工号" align="center" prop="teacherId" />
      <el-table-column label="教师职称" align="center" prop="title">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.title" :value="scope.row.title"/>
        </template>
      </el-table-column>
      <el-table-column label="教师职务" align="center" prop="position">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.position" :value="scope.row.position"/>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gender" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idNumber" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.education" :value="scope.row.education"/>
        </template>
      </el-table-column>
      <el-table-column label="授课课程" align="center" prop="teachingCourses" />
      <el-table-column label="教师照片" align="center" prop="teacherPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.teacherPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teacher:teacher:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teacher:teacher:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师姓名" />
        </el-form-item>
        <el-form-item label="教师工号" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师工号" />
        </el-form-item>
        <el-form-item label="教师职称" prop="title">
          <el-select v-model="form.title" placeholder="请选择教师职称">
            <el-option
              v-for="dict in dict.type.title"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师职务" prop="position">
          <el-select v-model="form.position" placeholder="请选择教师职务">
            <el-option
              v-for="dict in dict.type.position"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in dict.type.gender"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-radio-group v-model="form.education">
            <el-radio
              v-for="dict in dict.type.education"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="授课课程" prop="teachingCourses">
          <el-input v-model="form.teachingCourses" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="教师照片" prop="teacherPhoto">
          <image-upload 
          v-model="form.teacherPhoto"
          :limit="1"
          :file-size="2"
          :is-show-tip="true"
          :file-type="['jpg','jpeg','png']"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeacher, getTeacher, delTeacher, addTeacher, updateTeacher } from "@/api/teacher/teacher"
import getOssPolicy from '@/api/classinfo/classinfo'
export default {
  name: "Teacher",
  components: { getOssPolicy },
  dicts: ['title', 'position', 'gender', 'education'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师基本信息表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        teacherId: null,
        title: null,
        position: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teacherName: [
          { required: true, message: "教师姓名不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "教师工号不能为空", trigger: "blur" },
          {
            // 修改正则表达式，只允许 6 位数字
            pattern: /^\d{6}$/, 
            message: '教师工号必须是 6 位数字',
            trigger: 'blur'
          }
        ],
        title: [
          { required: true, message: "教师职称不能为空", trigger: "change" }
        ],
        position: [
          { required: true, message: "教师职务不能为空", trigger: "change" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        idNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          {
            pattern: /^\d{18}$/,
            message: '请输入 18 位有效身份证号',
            trigger: 'blur'
          }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
            pattern: /^\d{11}$/,
            message: '请输入 11 位有效手机号',
            trigger: 'blur'
          }
        ],
        nativePlace: [
          { required: true, message: "籍贯不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "学历不能为空", trigger: "change" }
        ],
        teacherPhoto: [
          { required: true, message: "教师照片不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询教师基本信息列表 */
    getList() {
      this.loading = true
      listTeacher(this.queryParams).then(response => {
        this.teacherList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        serialNumber: null,
        teacherName: null,
        teacherId: null,
        title: null,
        position: null,
        gender: null,
        idNumber: null,
        contactPhone: null,
        nativePlace: null,
        education: null,
        teachingCourses: null,
        teacherPhoto: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加教师基本信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const serialNumber = row.serialNumber || this.ids
      getTeacher(serialNumber).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改教师基本信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNumber != null) {
            updateTeacher(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addTeacher(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNumbers = row.serialNumber || this.ids
      this.$modal.confirm('是否确认删除教师基本信息编号为"' + serialNumbers + '"的数据项？').then(function() {
        return delTeacher(serialNumbers)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/teacher/export', {
        ...this.queryParams
      }, `teacher_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
